﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using TaskKernel.DTO;
using TaskWebApplicationUI.PresenterLayer;

namespace TaskWebApplicationUI
{
    public interface IProposalFormView : IAbstractView
    {
        IList<UniversalModelDDL> Templates { set; }
        Guid SelectedTemplate { get; set; }
        ListDictionary UserValues { get; }
        string ErrorMessage { set; }
        string TemplateHtml { set; get; }
        string TemplateResourceName { set; get; }
        string IPAddress { get; }
    }

    public partial class ProposalForm : ViewPage<IProposalFormView, ProposalPresenter>, IProposalFormView
    {
        private ListDictionary userValues;

        #region IProposalFormView Members

        public ListDictionary UserValues
        {
            get
            {
                userValues = new ListDictionary();
                IEnumerator monEnum = HttpContext.Current.Request.Params.GetEnumerator();
                while (monEnum.MoveNext())
                {
                    if (monEnum.Current.ToString().Contains("form_"))
                    {
                        userValues.Add(monEnum.Current.ToString(),
                                       HttpContext.Current.Request.Params[monEnum.Current.ToString()]);
                    }
                }
                return userValues;
            }
        }

        public string IPAddress
        {
            get
            {
                return
                    HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
        }

        public string ErrorMessage
        {
            set { ErrorMessageLabel.Text = value; }
        }

        public string DepartmentName
        {
            set { Department.Text = value; }
        }

        public string TemplateResourceName
        {
            get { return (string) ViewState["TemplateResourceName"]; }
            set { ViewState["TemplateResourceName"] = value; }
        }

        public IList<UniversalModelDDL> Templates
        {
            set
            {
                TemplatesDDL.DataSource = value;
                TemplatesDDL.DataTextField = "Name";
                TemplatesDDL.DataValueField = "ID";
                TemplatesDDL.DataBind();
            }
        }

        public string TemplateHtml
        {
            set { OutputHtmlText.Text = value; }
            get { return OutputHtmlText.Text; }
        }

        public Guid SelectedTemplate
        {
            set
            {
                TemplatesDDL.ClearSelection();
                TemplatesDDL.Items.FindByValue(value.ToString()).Selected = true;
            }
            get { return new Guid(TemplatesDDL.SelectedItem.Value); }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            //    if (Request.QueryString["User"] != null) LogonUser = Request.QueryString["User"];
                Presenter.OnViewInitialized();
            }
        }

        protected void TemplatesDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            Presenter.OnViewLoaded();
        }

        protected void LinkButton1_Click(object sender, ImageClickEventArgs e)
        {
            Presenter.OnSave();
        }
    }
}